<?php

namespace App\Manage;


use App\Service\Base\Manage;

class UserAgentManage extends Manage
{

    // 基本配置
    public function initialize()
    {
        parent::initialize();

        $this->model = 'UserAgent';
    }

    // 列表
    // 添加
    // 修改

    // 审核
    public function onVerify()
    {
        $_id = $this->id();
        if(empty($_id)){
            return $this->_returnFail('请指定删除记录');
        }

        $verify = $this->option()->getData('value', null, 'intval');
        $model  = $this->checkModel($this->model);
        $result = $model->onVerify($_id, $verify);
        if($result === false){
            return $this->_returnFail($model->getError());
        }

        return $result;
    }

    // 软删除
    protected function toSoft()
    {
        $_id = $this->id();
        if(empty($_id)){
            return $this->_returnFail('请指定删除记录');
        }

        $where = $this->delWhere();
        if($where === false){
            return $this->_returnFail($this->error);
        }

        $model  = $this->checkModel($this->model);
        $result = $model->isCache(true)->oneEdit($where, ['verify' => -1]);
        if($result === false){
            return $this->_returnFail($model->getError());
        }

        return $result;
    }

    // 列表查询
    protected function listWhere()
    {
        $option  = $this->option();
        $where   = [['verify', '>=', 0]];
        $bang    = [];
        $c_id    = $option->getData('course_id', 0, 'intval');
        $uid     = $option->getData('uid', 0, 'intval');
        $_type   = $option->getData('agent_type', null, 'trim');
        $_verify = $option->getData('verify', null, 'intval');
        $is_prop = $option->getData('is_propose', null, 'intval');
        $name    = $option->getData('agent_name', null, 'trim');
        $keyword = $option->getData('keyword', null, 'trim');
        if($c_id){
            $where[] = ['course_id', '=', $c_id];
            $bang[]  = 'course_id';
        }
        if($uid){
            $where[] = ['uid', '=', $uid];
            $bang[]  = 'uid';
        }
        if(!is_null($_verify)){
            $where[] = ['verify', '=', $_verify];
            $bang[]  = 'verify';
        }
        if(!is_null($is_prop)){
            $where[] = ['is_propose', '=', $is_prop];
            $bang[]  = 'is_propose';
        }
        if(!is_null($_type)){
            $where[] = ['agent_type', '=', $_type];
            $bang[]  = 'agent_type';
        }
        if($name){
            $where[] = ['agent_name', 'like', "%".$name."%"];
            $bang    = false;
        }
        if($keyword){
            $bang = false;
            //$where[] = $this->orWhere('username|nickname|mobile', 'like', $keyword);
        }

        // 缓存
        $this->bang = $bang;

        return $where;
    }


    // 添加之前
    protected function _before_add()
    {
        $_end = mktime(0, 0, 0, date("m"), date("d") + 1, date("Y") + 1);
        $opt  = $this->option();
        $data = [
            //'id'           => $opt->getData("id", null, "trim"),
            'uid'          => $opt->getData("uid", null, "trim"),
            'agent_type'   => $opt->getData("agent_type", null, "trim"),
            'agent_name'   => $opt->getData("agent_name", null, "trim"),
            'city'         => $opt->getData("city", null, "trim"),
            'grade'        => $opt->getData("grade", null, "trim"),
            'address'      => $opt->getData("address", null, "trim"),
            'telephone'    => $opt->getData("telephone", null, "trim"),
            'description'  => $opt->getData("description", null, "trim"),
            'license_img'  => $opt->getData("license_img", null, "trim"),
            'school_allow' => $opt->getData("school_allow", null, "trim"),
            'form_time'    => $opt->getData("form_time", LARAVEL_TIME, "trim"),
            'end_time'     => $opt->getData("end_time", $_end, "trim"),
            'is_pay'       => $opt->getData("is_pay", null, "intval"),
            'verify'       => $opt->getData("verify", null, "intval"),
            //'ctime'        => $opt->getData("ctime", null, "trim"),
            //'mtime'        => $opt->getData("mtime", null, "trim"),
        ];

        return $data;
    }

    // 修改之前
    protected function _before_edit()
    {
        $opt  = $this->option();
        $data = [
            //'id'           => $opt->getData("id", null, "trim"),
            'uid'          => $opt->getData("uid", null, "trim"),
            'agent_type'   => $opt->getData("agent_type", null, "trim"),
            'agent_name'   => $opt->getData("agent_name", null, "trim"),
            'city'         => $opt->getData("city", null, "trim"),
            'grade'        => $opt->getData("grade", null, "trim"),
            'address'      => $opt->getData("address", null, "trim"),
            'telephone'    => $opt->getData("telephone", null, "trim"),
            'description'  => $opt->getData("description", null, "trim"),
            'license_img'  => $opt->getData("license_img", null, "trim"),
            'school_allow' => $opt->getData("school_allow", null, "trim"),
            'form_time'    => $opt->getData("form_time", null, "trim"),
            'end_time'     => $opt->getData("end_time", null, "trim"),
            'is_pay'       => $opt->getData("is_pay", null, "intval"),
            'verify'       => $opt->getData("verify", null, "intval"),
            //'ctime'        => $opt->getData("ctime", null, "trim"),
            //'mtime'        => $opt->getData("mtime", null, "trim"),
        ];

        return $data;
    }

}


